<?php

namespace App\Admin\Controllers;

use App\Models\DispatchRule;
use App\Models\Project;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProjectController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Project(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
//            $grid->column('rule_type');
            $grid->column('rules', '规则')
                ->display(function ($val) {
                    return count($val) . '条规则';
                })
                ->label();
            $grid->column('customer_type_text', '绑定建档类型');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Project(), function (Show $show) {
            $show->field('id');
            $show->field('name');
//            $show->field('rule_type');
            $show->field('rule');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Project(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->radio('rule_type')
                ->options(DispatchRule::RULE_TYPE_OPTIONS)
                ->default(0);

            $form->table('rules', '规则', function (Form\NestedForm $form) {
                $form->select('field', '字段')
                    ->options(DispatchRule::CLUE_FIELDS)
                    ->required();
                $form->select('operator', '条件')->options(DispatchRule::OPERATOR)
                    ->default('like')
                    ->required();

                $form->text('value', '值')
                    ->required();
            })->default([
                [
                    'field' => '',
                    'operator' => '',
                    'value' => '',
                ]
            ]);

            $form->select('customer_type', '绑定建档类型')
                ->options(Project::getCustomerList())
                ->required();

            $form->hidden('customer_type_text');
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function (Form $form) {
                if ($customerType = $form->input('customer_type')) {
                    $list = Project::getCustomerList();
                    $form->input('customer_type_text', data_get($list, $customerType));
                }
                if (!request()->exists(['_file_', 'upload_column', 'size'])) {
                    $address = collect($form->input('rules'))->filter(function ($item) {
                        return !$item['_remove_'] && $item['field'] && $item['operator'] && $item['value'];
                    });
                    if (!$address->count()) {
                        $form->responseValidationMessages('rules', '规则不能为空');
                    }
                }
            });
        });
    }
}
